package com.yg.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yg.config.FilePathConfig;
import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.QualityTemplate;
import com.yg.entity.singleEntity.*;
import com.yg.listener.QualityReadListener;
import com.yg.mapper.QualityCourseMapper;
import com.yg.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 对应sheet06，此表是质量工程表课程，对应着表中的课程内容 服务实现类
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@Service
public class QualityCourseServiceImpl extends ServiceImpl<QualityCourseMapper, QualityCourse> implements QualityCourseService {

    @Autowired
    private FilePathConfig config;

    @Autowired
    private QualityProfessionalService qualityProfessionalService;

    @Autowired
    private QualityTeacherService qualityTeacherService;

    @Autowired
    private QualityTeamService qualityTeamService;

    @Autowired
    private QualityTitleService qualityTitleService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private QualityCourseMapper qualityCourseMapper;

    /**
     * 读取表
     *
     * @param file
     * @param r
     */
    @Transactional
    @Override
    public void readExcel(MultipartFile file, R r) {

        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, QualityTemplate.class, new QualityReadListener(teacherService, this, qualityProfessionalService, qualityTeacherService, qualityTeamService, qualityTitleService, r)).headRowNumber(3).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 写表格
     * @param response
     */
    @Transactional
    @Override
    public void writeExcel(HttpServletResponse response) {


        List<QualityTemplate> qualityTemplateList = getAllData2();

        System.out.println("----------------------------------------------------------------");
        qualityTemplateList.forEach(e -> {
            System.out.println("qualityTemplateList：" + e.toString());
        });

        //模板文件
        String fileTemplate = config.getWriteExcelFilePath() + "sheet06Template.xlsx";


        try {
            //导出(匿名内部类SheetWriteHandler)
            response.setHeader("Content-disposition", "attachment;filename=" + "sheet06Write.xlsx");
            EasyExcel.write(response.getOutputStream()).withTemplate(fileTemplate).registerWriteHandler(new SheetWriteHandler() {

                @Override
                public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

                }

                @Override
                public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
                    //日志
                }


            }).sheet().doFill(qualityTemplateList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private List<QualityTemplate> getAllData2() {
        int maxLength = 0;

        //校级开放课程
        List<QualityCourse> universityQualityCourses = qualityCourseMapper.getQualityCourse("校级在线开放课程");
        maxLength = universityQualityCourses.size() > maxLength ? universityQualityCourses.size() : maxLength;

        //省级一流课程
        List<QualityCourse> provincialQualityCourses = qualityCourseMapper.getQualityCourse("省级以上一流课程");
        maxLength = provincialQualityCourses.size() > maxLength ? provincialQualityCourses.size() : maxLength;

        //省级精品课程
        List<QualityCourse> provincialGoodsQualityCourses = qualityCourseMapper.getQualityCourse("省级以上精品课程");
        maxLength = provincialGoodsQualityCourses.size() > maxLength ? provincialGoodsQualityCourses.size() : maxLength;

        //省级以上特色专业
        List<QualityProfessional> characteristicsProfessionales = qualityCourseMapper.getQualityProfessional("省级以上特色专业");
        maxLength = characteristicsProfessionales.size() > maxLength ? characteristicsProfessionales.size() : maxLength;

        //省级以上一流专业
        List<QualityProfessional> provincialProfessionales = qualityCourseMapper.getQualityProfessional("省级以上一流专业");
        maxLength = provincialProfessionales.size() > maxLength ? provincialProfessionales.size() : maxLength;

        //省级以上教学名师
        List<QualityTeacher> qualityTeacherList = qualityTeacherService.list(null);
        maxLength = qualityTeacherList.size() > maxLength ? qualityTeacherList.size() : maxLength;

        //省级以上教学团队
        List<QualityTeam> provincialTeames = qualityCourseMapper.getQualityTeam("省级以上教学团队");
        maxLength = provincialTeames.size() > maxLength ? provincialTeames.size() : maxLength;

        //校级教学团队
        List<QualityTeam> universityTeames = qualityCourseMapper.getQualityTeam("校级教学团队");
        maxLength = universityTeames.size() > maxLength ? universityTeames.size() : maxLength;

        //校级教学称号
        List<QualityTitle> qualityTitleList = qualityTitleService.list(null);
        maxLength = qualityTitleList.size() > maxLength ? qualityTitleList.size() : maxLength;

        List<QualityTemplate> qualityTemplateList = new ArrayList<>();

        for (int i = 0; i < maxLength; i++) {

            QualityTemplate qualityTemplate = new QualityTemplate();

            //校级开放课程
            try {
                QualityCourse universityQualityCourse = universityQualityCourses.get(i);
                qualityTemplate.setUniversityLevelOpenOnlineCoursesName(universityQualityCourse.getName());
                qualityTemplate.setUniversityLevelOpenOnlineCoursesTime(universityQualityCourse.getTime());
                System.out.println(universityQualityCourse.toString());
            } catch (Exception e) {
            }

            //省级一流课程
            try {
                QualityCourse provincialQualityCourse = provincialQualityCourses.get(i);
                qualityTemplate.setFirstClassCoursesAtProvincialLevelOrAboveName(provincialQualityCourse.getName());
                qualityTemplate.setFirstClassCoursesAtProvincialLevelOrAboveTime(provincialQualityCourse.getTime());
                System.out.println(provincialQualityCourse.toString());
            } catch (Exception e) {
            }


            //省级精品课程
            try {
                QualityCourse provincialGoodsQualityCourse = provincialGoodsQualityCourses.get(i);
                qualityTemplate.setExcellentCoursesAboveTheProvincialLevelName(provincialGoodsQualityCourse.getName());
                qualityTemplate.setExcellentCoursesAboveTheProvincialLevelTime(provincialGoodsQualityCourse.getTime());
                System.out.println(provincialGoodsQualityCourse.toString());
            } catch (Exception e) {
            }


            //省级以上特色专业
            try {
                QualityProfessional characteristicsProfessional = characteristicsProfessionales.get(i);
                qualityTemplate.setSpecialtyAtProvincialLevelOrAboveName(characteristicsProfessional.getName());
                qualityTemplate.setSpecialtyAtProvincialLevelOrAboveTime(characteristicsProfessional.getTime());
                System.out.println(characteristicsProfessional.toString());
            } catch (Exception e) {
            }


            //省级以上一流专业
            try {
                QualityProfessional provincialProfessional = provincialProfessionales.get(i);
                qualityTemplate.setFirstClassMajorAtProvincialLevelOrAboveName(provincialProfessional.getName());
                qualityTemplate.setFirstClassMajorAtProvincialLevelOrAboveTime(provincialProfessional.getTime());
                System.out.println(provincialProfessional.toString());
            } catch (Exception e) {
            }

            //省级以上教学名师
            try {
                QualityTeacher qualityTeacher = qualityTeacherList.get(i);
                qualityTemplate.setFamousTeachingTeachersAboveTheProvincialLevelName(qualityTeacher.getName());
                qualityTemplate.setFamousTeachingTeachersAboveTheProvincialLevelTime(qualityTeacher.getTime());
                System.out.println(qualityTeacher.toString());
            } catch (Exception e) {
            }

            //省级以上教学团队
            try {
                QualityTeam qualityTeam = provincialTeames.get(i);
                qualityTemplate.setTeachingTeamAboveTheProvincialLevelName(qualityTeam.getName());
                qualityTemplate.setTeachingTeamAboveTheProvincialLevelTime(qualityTeam.getTime());
                System.out.println(qualityTeam.toString());
            } catch (Exception e) {
            }

            //校级教学团队
            try {
                QualityTeam universityTeam = universityTeames.get(i);
                qualityTemplate.setUniversityLevelTeachingTeamName(universityTeam.getName());
                qualityTemplate.setUniversityLevelTeachingTeamTime(universityTeam.getTime());
                System.out.println(universityTeam.toString());
            } catch (Exception e) {
            }


            //校级教学称号
            try {
                QualityTitle qualityTitle = qualityTitleList.get(i);
                qualityTemplate.setUniversityLevelTeachingTitleName(qualityTitle.getName());
                qualityTemplate.setUniversityLevelTeachingTitleTime(qualityTitle.getTime());
                qualityTemplate.setUniversityLevelTeachingTitleTitle(qualityTitle.getTitle());
                System.out.println(qualityTitle.toString());
            } catch (Exception e) {
            }

            qualityTemplate.setNums(i + 1 + "");
            qualityTemplateList.add(qualityTemplate);

        }

        return qualityTemplateList;
    }
}
